//
//  COStreamerReusableView.swift
//  CocoProject
//
//  Created by Vill on 2023/11/15.
//

import UIKit

class COStreamerReusableView: UICollectionReusableView {
    
    var tagView :DGTezTagView?
    var progressView:DGNProgressView?
    var timer : Timer?
    var progress = 0.0
    
    // 头像
    lazy var iconImg: UIImageView = {
        let icon = UIImageView()
        icon.contentMode = .scaleAspectFill
        icon.layer.cornerRadius = PTW(75)
        icon.layer.masksToBounds = true
        return icon
    }()
    
    // 昵称
    lazy var nameLbl: UILabel = {
        let name = UILabel()
        name.textColor = UIColor.white
        name.font = UIFont.pingfangFont(ofSize:PTW(18), weight: .medium)
        return name
    }()
    
    // 分割线
    lazy var lineLbl: UILabel = {
        let age = UILabel()
        age.backgroundColor = UIColor.colorWithHex("#FFFFFF", 0.1)
        return age
    }()
    
    // 热度
    lazy var hotLbl: UILabel = {
        let name = UILabel()
        name.textColor = UIColor.white.withAlphaComponent(0.5)
        name.font = UIFont.pingfangFont(ofSize: PTW(12), weight: .medium)
        return name
    }()
    
    // 年龄
    lazy var ageLbl: UILabel = {
        let name = UILabel()
        name.textColor = UIColor.white.withAlphaComponent(0.5)
        name.font = UIFont.pingfangFont(ofSize: PTW(13), weight: .regular)
        return name
    }()
        
    // 位置
    lazy var locationImg: UIImageView = {
        let name = UIImageView()
        name.image = KImaged("coco_profile_location")
        return name
    }()
    
    // 位置
    lazy var locationLbl: UILabel = {
        let name = UILabel()
        name.textColor = UIColor.white.withAlphaComponent(0.5)
        name.font = UIFont.pingfangFont(ofSize: PTW(13), weight: .regular)
        return name
    }()

    lazy var bottomLineLbl: UILabel = {
        let name = UILabel()
        name.backgroundColor =  UIColor.colorWithHex("#FF4D95")
        return name
    }()
    
    lazy var bottomPhotoLbl: UILabel = {
        let name = UILabel()
        name.text = "Photo"
        name.textColor = UIColor.white
        name.font = UIFont.pingfangFont(ofSize: PTW(20), weight: .medium)
        return name
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        creatSubviewUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    var headModel = DGChatUserInfoModel(){
        didSet{
            setContents()
        }
    }
    
    func setContents(){
        self.iconImg.sd_setUrlImage(headModel.profilePhoto, placeholder: KImaged("coco_icon_holder"))
        self.nameLbl.text = headModel.name
        self.hotLbl.text = "🔥 \(headModel.fans ?? 0) Fans"
        self.ageLbl.text = "🎂 \(headModel.age)"
        self.locationLbl.text = headModel.country
        if (self.tagView != nil){
            self.tagView!.removeFromSuperview()
            self.tagView = nil
        }
        let tagViewHeight = DGTezTagView.getTotalHeightWithValuesAndWords(words: headModel.tagArrs, sMargin: PTW(50), HMargin: PTW(25), IMargin: PTW(5))
        let tagView = DGTezTagView(frame: CGRectMake(0, PTW(225), SCREENWIDTH, tagViewHeight), strArr: headModel.tagArrs)
        tagView.tagHeight = PTW(25)
        tagView.sideMargin = PTW(50)
        tagView.tagInsideMargin = PTW(5)
        tagView.alignCenter = true
        self.addSubview(tagView)
        self.tagView = tagView
        
        if (self.progressView != nil){
            self.progressView!.removeFromSuperview()
            self.progressView = nil
        }
        let prog = DGNProgressView(frame: CGRectMake(PTW(45), PTW(245) + tagViewHeight, SCREENWIDTH - PTW(100), PTW(28)))
        self.addSubview(prog)
        self.progressView = prog
        self.progress = (Double(headModel.resCount) ?? 0)/100.0
        self.timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(valuechange), userInfo: nil, repeats: true)
        RunLoop.main.add(self.timer!, forMode: .common)
    }

    @objc func valuechange(){
        self.progressView?.progress += 0.01
        if(self.progressView?.progress ?? 0 >= self.progress){
            removeTimer()
        }
    }
    
    func removeTimer(){
        self.timer?.invalidate()
        timer = nil
    }
    
    deinit{
        self.removeTimer()
    }
    
    func setFans(fansNum:NSInteger){
        self.hotLbl.text = "🔥 \(fansNum) Fans"
    }
    
    func creatSubviewUI(){
        addSubview(self.iconImg)
        addSubview(self.nameLbl)
        addSubview(self.hotLbl)
        addSubview(self.lineLbl)
        addSubview(self.ageLbl)
        addSubview(self.locationImg)
        addSubview(self.locationLbl)
        addSubview(self.bottomLineLbl)
        addSubview(self.bottomPhotoLbl)
        
        self.iconImg.snp.makeConstraints { make in
            make.size.equalTo(PTW(150))
            make.centerX.top.equalToSuperview()
        }
        self.nameLbl.snp.makeConstraints { make in
            make.right.equalTo(self.iconImg.snp.centerX).offset(-PTW(3))
            make.top.equalTo(self.iconImg.snp.bottom).offset(PTW(10))
            make.height.equalTo(PTW(25))
        }
        self.hotLbl.snp.makeConstraints { make in
            make.left.equalTo(self.iconImg.snp.centerX).offset(PTW(3))
            make.centerY.equalTo(self.nameLbl)
        }
        self.lineLbl.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(PTW(1), PTW(10)))
            make.centerX.equalToSuperview()
            make.top.equalTo(self.nameLbl.snp.bottom).offset(PTW(8))
        }
        self.ageLbl.snp.makeConstraints { make in
            make.centerY.equalTo(self.lineLbl)
            make.right.equalTo(self.lineLbl.snp.left).offset(-PTW(10))
        }
        self.locationImg.snp.makeConstraints { make in
            make.centerY.equalTo(self.lineLbl)
            make.size.equalTo(PTW(15))
            make.left.equalTo(self.lineLbl.snp.right).offset(PTW(10))
        }
        self.locationLbl.snp.makeConstraints { make in
            make.centerY.equalTo(self.lineLbl)
            make.width.equalTo(PTW(120))
            make.left.equalTo(self.locationImg.snp.right).offset(PTW(2))
        }
        self.bottomLineLbl.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(PTW(27), PTW(3)))
            make.left.equalToSuperview().offset(PTW(11))
            make.bottom.equalToSuperview()
        }
        self.bottomPhotoLbl.snp.makeConstraints { make in
            make.left.equalTo(self.bottomLineLbl)
            make.bottom.equalTo(self.bottomLineLbl.snp.top).offset(-PTW(2))
        }
    }
}
